<?php
class Dropdown extends InputField
{
	private $options = array();
	private $sticky;
	private $cleared = false;
	
	public function __construct($name, $value = null, $sticky = true)
	{
		$this->setName($name);
		$this->setValue($value);
		$this->sticky = $sticky;
		
	}
	
	public function addOptions($data)
	{
		foreach ($data as $label=>$value)
		{
			$this->addOptions($label, $value);
		}
	}
	
	public function getValue()
	{
		return $_POST[$this->getName()];
	}
	
	public function addOption($label, $value)
	{
		$this->options[$label] = $value;
	}
	
	public function clear()
	{
		$this->cleared = true;
	}
	
	/**
	 * 
	 * @see HTMLElement::getHTML()
	 */
	public function getHTML() {
		$html = '<select ';
		$html = $this->addAttributes($html);
		$html .= '>';
		
		$html .= "\n\t";
		
		foreach ($this->options as $label=>$value)
		{			
			$html .= "\n\t\t".'<option value="'.$value.'"';
			
			if(!isset($_POST[$this->getName()]) && $this->value== $value)
			{
				$html .= ' selected="selected"';					
			}
			elseif(!$this->cleared && isset($_POST[$this->getName()]) &&  $_POST[$this->getName()] == $value)
			{
				$html .= ' selected="selected"';
			}
			$html .='>'.$label.'</option>';
		}
		$html .= "\n\t";
		$html .= '</tr>';
		
		return $html;
	}
}